﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.Cryptography.CRC16
{
    public class XModemAlgorithm
    {
        public int ComputeCRC(byte[] buffer, int len)
        {
            int crc = 0;
            int i = 0;
            int j = 0;
            int d = 0;

            for (i = 0; i <= len - 1; i++)
            {
                d = buffer[i];
                crc = crc ^ (d << 8);
                for (j = 0; j <= 7; j++)
                {
                    if (((crc & 0x8000) != 0))
                    {
                        crc = (crc << 1) ^ 0x1021;
                    }
                    else
                    {
                        crc = (crc << 1);
                    }
                }
            }
            return crc & 0xffff;
        }
    }
}
